<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>dragscrollsupport.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_fx_dragscrollsupport.js.html">dragscrollsupport.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Class to support scrollable containers for drag and drop.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.fx.DragScrollSupport&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.Disposable&#39;);
<a name="line23"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom&#39;);
<a name="line25"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line26"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line27"></a>goog.require(&#39;goog.math.Coordinate&#39;);
<a name="line28"></a>goog.require(&#39;goog.style&#39;);
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>/**
<a name="line33"></a> * A scroll support class. Currently this class will automatically scroll
<a name="line34"></a> * a scrollable container node and scroll it by a fixed amount at a timed
<a name="line35"></a> * interval when the mouse is moved above or below the container or in vertical
<a name="line36"></a> * margin areas. Intended for use in drag and drop. This could potentially be
<a name="line37"></a> * made more general and could support horizontal scrolling.
<a name="line38"></a> *
<a name="line39"></a> * @param {Element} containerNode A container that can be scrolled.
<a name="line40"></a> * @param {number=} opt_margin Optional margin to use while scrolling.
<a name="line41"></a> * @param {boolean=} opt_externalMouseMoveTracking Whether mouse move events
<a name="line42"></a> *     are tracked externally by the client object which calls the mouse move
<a name="line43"></a> *     event handler, useful when events are generated for more than one source
<a name="line44"></a> *     element and/or are not real mousemove events.
<a name="line45"></a> * @constructor
<a name="line46"></a> * @extends {goog.Disposable}
<a name="line47"></a> * @see ../demos/dragscrollsupport.html
<a name="line48"></a> */
<a name="line49"></a>goog.fx.DragScrollSupport = function(containerNode, opt_margin,
<a name="line50"></a>                                     opt_externalMouseMoveTracking) {
<a name="line51"></a>  goog.Disposable.call(this);
<a name="line52"></a>
<a name="line53"></a>  /**
<a name="line54"></a>   * The container to be scrolled.
<a name="line55"></a>   * @type {Element}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.containerNode_ = containerNode;
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * Scroll timer that will scroll the container until it is stopped.
<a name="line62"></a>   * It will scroll when the mouse is outside the scrolling area of the
<a name="line63"></a>   * container.
<a name="line64"></a>   *
<a name="line65"></a>   * @type {goog.Timer}
<a name="line66"></a>   * @private
<a name="line67"></a>   */
<a name="line68"></a>  this.scrollTimer_ = new goog.Timer(goog.fx.DragScrollSupport.TIMER_STEP_);
<a name="line69"></a>
<a name="line70"></a>  /**
<a name="line71"></a>   * EventHandler used to set up and tear down listeners.
<a name="line72"></a>   * @type {goog.events.EventHandler}
<a name="line73"></a>   * @private
<a name="line74"></a>   */
<a name="line75"></a>  this.eventHandler_ = new goog.events.EventHandler(this);
<a name="line76"></a>
<a name="line77"></a>  /**
<a name="line78"></a>   * The current scroll delta.
<a name="line79"></a>   * @type {goog.math.Coordinate}
<a name="line80"></a>   * @private
<a name="line81"></a>   */
<a name="line82"></a>  this.scrollDelta_ = new goog.math.Coordinate();
<a name="line83"></a>
<a name="line84"></a>  /**
<a name="line85"></a>   * The container bounds.
<a name="line86"></a>   * @type {goog.math.Rect}
<a name="line87"></a>   * @private
<a name="line88"></a>   */
<a name="line89"></a>  this.containerBounds_ = goog.style.getBounds(containerNode);
<a name="line90"></a>
<a name="line91"></a>  /**
<a name="line92"></a>   * The margin for triggering a scroll.
<a name="line93"></a>   * @type {number}
<a name="line94"></a>   * @private
<a name="line95"></a>   */
<a name="line96"></a>  this.margin_ = opt_margin || 0;
<a name="line97"></a>
<a name="line98"></a>  /**
<a name="line99"></a>   * The bounding rectangle which if left triggers scrolling.
<a name="line100"></a>   * @type {goog.math.Rect}
<a name="line101"></a>   * @private
<a name="line102"></a>   */
<a name="line103"></a>  this.scrollBounds_ = opt_margin ?
<a name="line104"></a>      this.constrainBounds_(this.containerBounds_.clone()) :
<a name="line105"></a>      this.containerBounds_;
<a name="line106"></a>
<a name="line107"></a>  this.setupListeners_(!!opt_externalMouseMoveTracking);
<a name="line108"></a>};
<a name="line109"></a>goog.inherits(goog.fx.DragScrollSupport, goog.Disposable);
<a name="line110"></a>
<a name="line111"></a>
<a name="line112"></a>/**
<a name="line113"></a> * The scroll timer step in ms.
<a name="line114"></a> * @type {number}
<a name="line115"></a> * @private
<a name="line116"></a> */
<a name="line117"></a>goog.fx.DragScrollSupport.TIMER_STEP_ = 50;
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * The scroll step in pixels.
<a name="line122"></a> * @type {number}
<a name="line123"></a> * @private
<a name="line124"></a> */
<a name="line125"></a>goog.fx.DragScrollSupport.SCROLL_STEP_ = 8;
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * The suggested scrolling margin.
<a name="line130"></a> * @type {number}
<a name="line131"></a> */
<a name="line132"></a>goog.fx.DragScrollSupport.MARGIN = 32;
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * Whether scrolling should be constrained to happen only when the cursor is
<a name="line137"></a> * inside the container node.
<a name="line138"></a> * @type {boolean}
<a name="line139"></a> * @private
<a name="line140"></a> */
<a name="line141"></a>goog.fx.DragScrollSupport.prototype.constrainScroll_ = false;
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>/**
<a name="line145"></a> * Whether horizontal scrolling is allowed.
<a name="line146"></a> * @type {boolean}
<a name="line147"></a> * @private
<a name="line148"></a> */
<a name="line149"></a>goog.fx.DragScrollSupport.prototype.horizontalScrolling_ = true;
<a name="line150"></a>
<a name="line151"></a>
<a name="line152"></a>/**
<a name="line153"></a> * Sets whether scrolling should be constrained to happen only when the cursor
<a name="line154"></a> * is inside the container node.
<a name="line155"></a> * NOTE: If a margin is not set, then it does not make sense to
<a name="line156"></a> * contain the scroll, because in that case scroll will never be triggered.
<a name="line157"></a> * @param {boolean} constrain Whether scrolling should be constrained to happen
<a name="line158"></a> *     only when the cursor is inside the container node.
<a name="line159"></a> */
<a name="line160"></a>goog.fx.DragScrollSupport.prototype.setConstrainScroll = function(constrain) {
<a name="line161"></a>  this.constrainScroll_ = !!this.margin_ &amp;&amp; constrain;
<a name="line162"></a>};
<a name="line163"></a>
<a name="line164"></a>
<a name="line165"></a>/**
<a name="line166"></a> * Sets whether horizontal scrolling is allowed.
<a name="line167"></a> * @param {boolean} scrolling Whether horizontal scrolling is allowed.
<a name="line168"></a> */
<a name="line169"></a>goog.fx.DragScrollSupport.prototype.setHorizontalScrolling =
<a name="line170"></a>    function(scrolling) {
<a name="line171"></a>  this.horizontalScrolling_ = scrolling;
<a name="line172"></a>};
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * Constrains the container bounds with respect to the margin.
<a name="line177"></a> *
<a name="line178"></a> * @param {goog.math.Rect} bounds The container element.
<a name="line179"></a> * @return {goog.math.Rect} The bounding rectangle used to calculate scrolling
<a name="line180"></a> *     direction.
<a name="line181"></a> * @private
<a name="line182"></a> */
<a name="line183"></a>goog.fx.DragScrollSupport.prototype.constrainBounds_ = function(bounds) {
<a name="line184"></a>  var margin = this.margin_;
<a name="line185"></a>  if (margin) {
<a name="line186"></a>    var quarterHeight = bounds.height * 0.25;
<a name="line187"></a>    var yMargin = Math.min(margin, quarterHeight);
<a name="line188"></a>    bounds.top += yMargin;
<a name="line189"></a>    bounds.height -= 2 * yMargin;
<a name="line190"></a>
<a name="line191"></a>    var quarterWidth = bounds.width * 0.25;
<a name="line192"></a>    var xMargin = Math.min(margin, quarterWidth);
<a name="line193"></a>    bounds.top += xMargin;
<a name="line194"></a>    bounds.height -= 2 * xMargin;
<a name="line195"></a>  }
<a name="line196"></a>  return bounds;
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * Attaches listeners and activates automatic scrolling.
<a name="line202"></a> * @param {boolean} externalMouseMoveTracking Whether to enable internal
<a name="line203"></a> *     mouse move event handling.
<a name="line204"></a> * @private
<a name="line205"></a> */
<a name="line206"></a>goog.fx.DragScrollSupport.prototype.setupListeners_ = function(
<a name="line207"></a>    externalMouseMoveTracking) {
<a name="line208"></a>  if (!externalMouseMoveTracking) {
<a name="line209"></a>    // Track mouse pointer position to determine scroll direction.
<a name="line210"></a>    this.eventHandler_.listen(goog.dom.getOwnerDocument(this.containerNode_),
<a name="line211"></a>        goog.events.EventType.MOUSEMOVE, this.onMouseMove);
<a name="line212"></a>  }
<a name="line213"></a>
<a name="line214"></a>  // Scroll with a constant speed.
<a name="line215"></a>  this.eventHandler_.listen(this.scrollTimer_, goog.Timer.TICK, this.onTick_);
<a name="line216"></a>};
<a name="line217"></a>
<a name="line218"></a>
<a name="line219"></a>/**
<a name="line220"></a> * Handler for timer tick event, scrolls the container by one scroll step if
<a name="line221"></a> * needed.
<a name="line222"></a> * @param {goog.events.Event} event Timer tick event.
<a name="line223"></a> * @private
<a name="line224"></a> */
<a name="line225"></a>goog.fx.DragScrollSupport.prototype.onTick_ = function(event) {
<a name="line226"></a>  this.containerNode_.scrollTop += this.scrollDelta_.y;
<a name="line227"></a>  this.containerNode_.scrollLeft += this.scrollDelta_.x;
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Handler for mouse moves events.
<a name="line233"></a> * @param {goog.events.Event} event Mouse move event.
<a name="line234"></a> */
<a name="line235"></a>goog.fx.DragScrollSupport.prototype.onMouseMove = function(event) {
<a name="line236"></a>  var deltaX = this.horizontalScrolling_ ? this.calculateScrollDelta(
<a name="line237"></a>      event.clientX, this.scrollBounds_.left, this.scrollBounds_.width) : 0;
<a name="line238"></a>  var deltaY = this.calculateScrollDelta(event.clientY,
<a name="line239"></a>      this.scrollBounds_.top, this.scrollBounds_.height);
<a name="line240"></a>  this.scrollDelta_.x = deltaX;
<a name="line241"></a>  this.scrollDelta_.y = deltaY;
<a name="line242"></a>
<a name="line243"></a>  // If the scroll data is 0 or the event fired outside of the
<a name="line244"></a>  // bounds of the container node.
<a name="line245"></a>  if ((!deltaX &amp;&amp; !deltaY) ||
<a name="line246"></a>      (this.constrainScroll_ &amp;&amp;
<a name="line247"></a>       !this.isInContainerBounds_(event.clientX, event.clientY))) {
<a name="line248"></a>    this.scrollTimer_.stop();
<a name="line249"></a>  } else if (!this.scrollTimer_.enabled) {
<a name="line250"></a>    this.scrollTimer_.start();
<a name="line251"></a>  }
<a name="line252"></a>};
<a name="line253"></a>
<a name="line254"></a>
<a name="line255"></a>/**
<a name="line256"></a> * Gets whether the input coordinate is in the container bounds.
<a name="line257"></a> * @param {number} x The x coordinate.
<a name="line258"></a> * @param {number} y The y coordinate.
<a name="line259"></a> * @return {boolean} Whether the input coordinate is in the container bounds.
<a name="line260"></a> * @private
<a name="line261"></a> */
<a name="line262"></a>goog.fx.DragScrollSupport.prototype.isInContainerBounds_ = function(x, y) {
<a name="line263"></a>  var containerBounds = this.containerBounds_;
<a name="line264"></a>  return containerBounds.left &lt;= x &amp;&amp;
<a name="line265"></a>         containerBounds.left + containerBounds.width &gt;= x &amp;&amp;
<a name="line266"></a>         containerBounds.top &lt;= y &amp;&amp;
<a name="line267"></a>         containerBounds.top + containerBounds.height &gt;= y;
<a name="line268"></a>};
<a name="line269"></a>
<a name="line270"></a>
<a name="line271"></a>/**
<a name="line272"></a> * Calculates scroll delta.
<a name="line273"></a> *
<a name="line274"></a> * @param {number} coordinate Current mouse pointer coordinate.
<a name="line275"></a> * @param {number} min The coordinate value below which scrolling up should be
<a name="line276"></a> *     started.
<a name="line277"></a> * @param {number} rangeLength The length of the range in which scrolling should
<a name="line278"></a> *     be disabled and above which scrolling down should be started.
<a name="line279"></a> * @return {number} The calculated scroll delta.
<a name="line280"></a> * @protected
<a name="line281"></a> */
<a name="line282"></a>goog.fx.DragScrollSupport.prototype.calculateScrollDelta = function(
<a name="line283"></a>    coordinate, min, rangeLength) {
<a name="line284"></a>  var delta = 0;
<a name="line285"></a>  if (coordinate &lt; min) {
<a name="line286"></a>    delta = -goog.fx.DragScrollSupport.SCROLL_STEP_;
<a name="line287"></a>  } else if (coordinate &gt; min + rangeLength) {
<a name="line288"></a>    delta = goog.fx.DragScrollSupport.SCROLL_STEP_;
<a name="line289"></a>  }
<a name="line290"></a>  return delta;
<a name="line291"></a>};
<a name="line292"></a>
<a name="line293"></a>
<a name="line294"></a>/** @override */
<a name="line295"></a>goog.fx.DragScrollSupport.prototype.disposeInternal = function() {
<a name="line296"></a>  goog.fx.DragScrollSupport.superClass_.disposeInternal.call(this);
<a name="line297"></a>  this.eventHandler_.dispose();
<a name="line298"></a>  this.scrollTimer_.dispose();
<a name="line299"></a>};
</pre>


</body>
</html>
